package com.kiwilss.aopdemo.ui.livedata

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import com.kiwilss.aopdemo.R
import kotlinx.android.synthetic.main.activity_livedata.*

/**
 * @author : Administrator
 * e-mail : kiwilss@163.com
 * time   : 2021/02/01
 *  desc   :
 */
class LiveDataActivity : AppCompatActivity(R.layout.activity_livedata){

    val loginResult = MutableLiveData<Boolean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //创建观察者对象
        val nameObserver = Observer<String>{
            tvLiveDataOne.text = it
        }
        //订阅
        val testViewModel = ViewModelProvider(this)[TestViewModel::class.java]
        testViewModel.mCurrent!!.observe(this,nameObserver)


//        loginResult.observe(this@LiveDataActivity, Observer {
//        })
//        loginResult.observe(this) {
//            finish()
//        }

        //发送改变界面数据
        btnLiveDataOne.setOnClickListener {
            testViewModel.mCurrent!!.value = "aaaaaaaa"
        }

        val lowObserver = Observer<String> {
            tvLiveDataTwo.text = it
        }

        val lengthObserver = Observer<Int> {
            tvLiveDataThree.text = it.toString()
        }

        testViewModel.liveDataSwitch.observe(this,lowObserver)
        testViewModel.liveDataMap.observe(this,lengthObserver)



    }
}